<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form">
        <div class="layui-form-item" hidden>
            <label class="layui-form-label required">员工ID</label>
            <div class="layui-input-block">
                <input type="text" name="employeeId" lay-reqtext="员工ID不能为空" placeholder="请输入员工ID" value=""
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <img class="layadmin-homepage-pad-img" id="avatarPreview" src="/images/logo.png" width="96" height="96"
                style="margin-bottom: 10px;">
            <label class="layui-form-label required">个人头像</label>
            <div class="layui-input-block">
                <input type="input" name="avatar" style="display: none;" id="avatarUpload">
                <button class="layui-btn layui-btn-sm layui-btn-primary" id="uploadAvatarBtn">上传头像</button>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">全名</label>
            <div class="layui-input-inline">
                <input type="text" name="fullName" placeholder="请输入全名" class="layui-input" lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">电子邮件</label>
            <div class="layui-input-inline">
                <input type="text" name="email" placeholder="请输入电子邮件" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">电话号码</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" placeholder="请输入电话号码" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-inline">
                <select id="departmentSelect" name="department" lay-verify="required">
                    <!-- 这里将在JavaScript中动态生成选项 -->
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">职位</label>
            <div class="layui-input-inline">
                <input type="text" name="position" placeholder="请输入职位" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">入职日期</label>
            <div class="layui-input-inline">
                <input type="text" name="joinDate" id="joinDate" placeholder="请选择入职日期" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">薪资</label>
            <div class="layui-input-inline">
                <input type="text" name="salary" placeholder="请输入薪资" class="layui-input" lay-verify="number">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>

    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;

            var laydate = layui.laydate;

            // 渲染日期选择组件
            laydate.render({
                elem: '#joinDate', // 绑定到指定的input元素
                type: 'date', // 设置日期类型，可选值：year、month、date、time、datetime
                format: 'yyyy-MM-dd HH:mm:ss', // 设置日期格式
                // 其他配置项，可根据需求自行设置
            });

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                console.log(JSON.stringify(data.field))
                $.ajax({
                    url: 'http://localhost:8080/api/employee?operation=add',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(data.field),
                    success: function (res) {
                        if (res.code === 0) {
                            layer.alert('保存成功', {
                                title: '操作提示'
                            }, function () {
                                // 刷新父页面
                                parent.location.reload();
                                // 关闭弹出层
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.alert('保存失败，请重试', {
                                title: '操作提示',
                                icon: 2
                            });
                        }
                    },
                    error: function () {
                        layer.alert('请求失败，请重试', {
                            title: '操作提示',
                            icon: 2
                        });
                    }
                });
                return false;
            });


            var upload = layui.upload;

            // 监听上传按钮的点击事件
            $('#uploadAvatarBtn').on('click', function () {
                $('#avatarUpload').click(); // 触发文件选择框的点击事件
            });

            // 上传头像
            upload.render({
                elem: '#avatarUpload',
                url: 'http://localhost:8080/api/file/upload?operation=doPost', // 替换成实际的上传接口地址
                accept: 'images', // 只允许上传图片
                exts: 'jpg|png|gif|bmp|jpeg',
                done: function (res) {
                    // 直接使用后端返回的 URL 来更新头像预览
                    if (res.code === 0) {
                        var imageUrl = res.data;
                        $('#avatarPreview').attr('src', imageUrl);
                        // 更新隐藏的输入框的值
                        $('#avatarUpload').val(imageUrl);
                    } else {
                        layer.msg('上传失败', { icon: 2 });
                    }
                }
            });



            // 使用Ajax请求获取部门数据
            $.ajax({
                type: 'GET',
                url: 'http://localhost:8080/api/job_position?operation=getAllJobPositions',
                dataType: 'json',
                success: function (response) {
                    console.log(response.data)
                    if (response.code === 0) {
                        var jobPositions = response.data; // 假设响应中的数据字段名为data

                        // 获取下拉列表的 DOM 元素
                        var departmentSelect = document.getElementById('departmentSelect');

                        // 遍历响应数据，创建选项并添加到下拉列表中
                        for (var i = 0; i < jobPositions.length; i++) {
                            var option = document.createElement('option');
                            option.value = jobPositions[i].department; 
                            option.text = jobPositions[i].department; // 假设部门名称字段名为positionName
                            departmentSelect.appendChild(option);
                        }

                        // 使用layui的form模块重新渲染下拉列表
                        form.render('select');
                    } else {
                        // 请求失败，处理错误情况
                        console.error('请求失败：' + response.msg);
                    }
                },
                error: function (error) {
                    // 请求出错，处理错误情况
                    console.error('请求出错：' + JSON.stringify(error));
                }
            });

        });



    </script>
</body>

</html>